//
//  SearchResultsViewController.m
//  BVCMS Search
//
//  Created by Allen Thomas on 9/13/10.
//

#import "SearchResultsViewController.h"
#import "SearchResultDetailViewController.h"
#import "ResultCell.h"
#import "MoreResultsCell.h"

@implementation SearchResultsViewController

@synthesize resultsData, numberOfResults;

/* 
 performed when view is unloaded.
 */
- (void)viewDidUnload {
	self.resultsData = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

/*
 Release allocated memory.
 */
- (void)dealloc {
	[resultsData release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Data Source Methods
/*
 Return number of rows.
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
	if(numberOfResults > 20) {	   
		return [self.resultsData count] + 1;
	} else {
		return [self.resultsData count];
	}
}

/*
 Returns a UITableViewCell for a row.
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if( indexPath.row < 20 ) {
		ResultCell *cell = (ResultCell*)[tableView dequeueReusableCellWithIdentifier: @"ResultCellIdentifier"];
		
		if (cell == nil) {         
			cell = [[[NSBundle mainBundle] loadNibNamed:@"ResultCell" owner:self options:nil] objectAtIndex:0];
		}
		
		NSUInteger row = [indexPath row]; 
		
		NSDictionary *person = [resultsData objectAtIndex:row];
		
		cell.nameLabel.text = [person valueForKey:@"name"]; 
		cell.addressLabel.text = [person valueForKey:@"address"]; ;
		cell.cityStateZipLabel.text = [person valueForKey:@"citystatezip"]; ;
		cell.ageLabel.text = [person valueForKey:@"age"]; ;
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		
		[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
		return cell;
	} else {
		MoreResultsCell *cell = (MoreResultsCell *)[tableView dequeueReusableCellWithIdentifier:@"RemainingResults"];
		if (cell == nil) {
			cell = [[[NSBundle mainBundle] loadNibNamed:@"MoreResultsCell" owner:self options:nil] objectAtIndex:0];
		}
		
		cell.backgroundView.backgroundColor = [UIColor lightGrayColor];
		
		NSString *strResults = [[NSString alloc] initWithFormat:@"%d", numberOfResults - 20];
		cell.totalResults.text = strResults;
		[strResults release];
				
		[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
		
		return cell;
	}
}

/*
 Return the height of a cell.
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
	return kTableViewRowHeight;
}

#pragma mark - #pragma mark Table View Delegate Methods 
/*
 Triggered when the user selects a table row.
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
	if( indexPath.row < 20 ) {
		NSUInteger row = [indexPath row];
		NSLog(@"Row Selected: %d", row);
		
		NSDictionary *person = [resultsData objectAtIndex:row];
		
		SearchResultDetailViewController *searchResult = [[SearchResultDetailViewController alloc]
														  initWithNibName:@"SearchResultDetailView" bundle:[NSBundle mainBundle]];
		searchResult.title = [person valueForKey:@"name"];
		searchResult.person = person;
			
		[tableView deselectRowAtIndexPath:indexPath animated:YES];
		
		[self.navigationController pushViewController:searchResult animated:YES];
		[searchResult release];
	}
}

@end
